Skip to content

fix(utils): keep non-string instruct fields in Message JSON - #2216

Open
DZH166 wants to merge 1 commit into
FoundationAgents:mainfrom
DZH166:fix/serialize-nonstring-instruct
Open

DZH166 wants to merge 1 commit into
FoundationAgents:mainfrom
DZH166:fix/serialize-nonstring-instruct

Conversation

@DZH166

@DZH166 DZH166 commented Nov 21, 2026

Copy link
Copy Markdown

Problem

Message.instruct_content is serialized as {"class", "mapping", "value"} and rebuilt on load,
but actionoutout_schema_to_mapping() in metagpt/utils/serialize.py only maps string,
array<string> and array<array<string>> properties. Any integer, number, boolean or
object field of an ActionNode payload is left out of the mapping, so on
Message.model_validate_json(message.model_dump_json()) pydantic rebuilds the model without those
fields and the values are silently dropped. actionoutput_str_to_mapping() additionally raises
SyntaxError on the "(<class 'int'>, Ellipsis)" form because only the str case is
special-cased before eval().

ic = ActionNode.create_model_class("plan", {"page_count": (int, ...), "summary": (str, ...)})(page_count=7, summary="x")
msg = Message(content="x", instruct_content=ic)
Message.model_validate_json(msg.model_dump_json()).instruct_content.model_dump()
# -> {'summary': 'x'}   # page_count lost

Fix

  • actionoutout_schema_to_mapping() maps integer/number/boolean/object to
    int/float/bool/dict and array items to list[<mapped type>]; missing type/items
    keys are skipped instead of raising KeyError.
  • actionoutput_str_to_mapping() resolves builtin type strings by name through
    PY_TYPE_TO_MAPPING_STR instead of calling eval() on invalid Python syntax.

Tests

  • tests/metagpt/utils/test_serialize.py::test_serialize_and_deserialize_message_with_non_string_fields
    round-trips int/float/bool/dict/str payloads through both serialize_message/deserialize_message
    and model_dump_json/model_validate_json.
  • tests/metagpt/utils/test_serialize.py: 1 failed + 2 passed before the fix, 3 passed after.

This branch is waiting to be deployed

1 waiting deployment
unittest — 37df33c8 Waiting Nov 21, 2026 by DZH166 via build (3.9) #1780
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant